package com.example.demo.service;

import com.example.demo.model.User;
import com.example.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

// @Service：标识为服务组件
//业务服务
@Service
public class UserService {

    // 自动注入UserRepository
    @Autowired
    private UserRepository userRepository;

    // 事务注解（替代XML的事务配置）
    @Transactional
    public User saveUser(User user) {
        return userRepository.save(user);
    }

    public User findByUsername(String username) {
        return userRepository.findByUsername(username);
    }
}